<?php
/**
 * 算力流水表列表
 */
namespace app\common\model;

use think\facade\Db;
use think\Model;
use app\api\model\Member as MemberModel;

class ArithmeticalLog extends Model
{
    protected $name = 'arithmetical_log';
    public $type = [
        'Direct reward'  =>  [
            'english'   =>  'Direct reward',
            'traditional'=>'直推獎勵',
            'simplified'    =>  '直推奖励'
        ],
        'Indirect rewards'  =>  [
            'english'   =>  'Indirect rewards',
            'traditional'=>'間接獎勵',
            'simplified'    =>  '间推奖励'
        ],
        'Pledge deduction'  =>  [
            'english'   =>  'Pledge deduction',
            'traditional'=>'質押扣費',
            'simplified'    =>  '质押扣费'
        ],
        'produce'   =>  [
            'english'   =>  'Node output',
            'traditional'=>'節點產出',
            'simplified'    =>  '节点产出'
        ],
    ];

    /**
     * 根据ID查询算力流水详情
     * @param $id   轮播ID
     * @return Banner|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getDetails($id){
        return self::where('id',$id)->find();
    }

    /**
     * 获取所有算力流水
     * @param array $where 查询条件
     * @param string $order 排序
     * @return ArithmeticalLog[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getListAll($where = [],$order = 'id desc'){
        return self::where($where)->order($order)->select();
    }

    /**
     * 查询列表
     * @param int $page 页码
     * @param int $limit    页面数量
     * @param array $where 查询条件
     * @param string $order 排序
     * @return array
     * @throws \think\db\exception\DbException
     */
    public static function getList($page=1,$limit=20,$where=[],$order = 'id desc'){
        $data = self::where($where)->order($order)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
        return $data;
    }

    /**
     * 添加矿机流水记录
     * @param $mid 用户ID
     * @param $num 更新数量
     * @param string $type  更新类型 produce:矿机产出
     * @param string $remarks 注释
     * @param string $maorder_id 矿机ID
     * @param string $relation_id 关联ID
     * @return false|int|string
     */
    public static function add($mid,$num,$type = 'produce',$remarks = '',$maorder_id = '',$relation_id = ''){
        //钱包
        $member_wallet = MemberModel::wallet($mid);
        //计算更新后余额
        $after_num = bcadd($member_wallet['adc'],$num,8);

        $data = [
            'mid'   =>  $mid,
            'type'  =>  $type,
            'relation_id'   =>  $relation_id,
            'before_num'    =>  $member_wallet['adc'],
            'num'   =>  $num,
            'after_num' =>  $after_num,
            'maorder_id'    =>  $maorder_id,
            'remarks'   =>  $remarks,
            'add_time'  =>  date('Y-m-d H:i:s',time())
        ];

        $id = self::insertGetId($data);
        MemberModel::where('id',$mid)->update(['adc'=>$after_num]);

        return $id;
    }

}